package kibahed.uni.fileinteractions;

import java.awt.Desktop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.util.JAXBSource;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import kibahed.uni.setcomperator.Entry;
import kibahed.uni.setcomperator.HashSet_TreeSet_Comperator;
import kibahed.uni.setcomperator.TestClass;
import kibahed.uni.setcomperator.TestOperation;
import kibahed.uni.setcomperator.TestType;

public class XMLParser {

	@SuppressWarnings("rawtypes")
	private static Class[] classes;

	public static JAXBContext getJAXBContext() {
		fillClasses();
		JAXBContext jc = null;
		try {
			jc = JAXBContext.newInstance(classes);
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jc;
	}

	public static Object loadObject(File file) {
		Object loadedSystem = null;
		try {
			Object o = FileInteractions.loadMarshalledObject(file,
					getJAXBContext());

			loadedSystem = o;

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return loadedSystem;
	}

	public static boolean saveObject(File file, Object o) {

		JAXBContext jc;
		try {
			jc = getJAXBContext();

			Marshaller m = jc.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			try {
				m.marshal(
						o,
						new FileOutputStream(file.getPath().replaceAll(".xml",
								"")
								+ ".xml"));
			} catch (FileNotFoundException ex) {
				ex.printStackTrace();
			}
		} catch (JAXBException ex) {
			ex.printStackTrace();
		}

		createHTML(file, o);

		return false;
	}

	private static void createHTML(File file, Object o) {

		TransformerFactory tf = TransformerFactory.newInstance();
		StreamSource xslt = new StreamSource("res/firstTransform.xsl");
		Transformer transformer = null;
		File f2 = FileInteractions.openSaveDiaolg(file.getPath());
		
		StreamResult result = null;
		try {
			result = new StreamResult(new FileOutputStream(f2.getPath().replaceAll(".html","")
					+ ".html"));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		try {
			
			JAXBSource source = new JAXBSource(getJAXBContext(), o);
			transformer = tf.newTransformer(xslt);
			transformer.transform(source, result);
			
			Desktop.getDesktop().open(new File(f2.getAbsolutePath()+".html"));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
		
		
	}

	private static void fillClasses() {
		classes = new Class[5];
		classes[0] = Entry.class;
		classes[1] = HashSet_TreeSet_Comperator.class;
		classes[2] = TestClass.class;
		classes[3] = TestOperation.class;
		classes[4] = TestType.class;
	}

}
